package com.vast.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vast.admin.annotation.UseToken;
import com.vast.admin.entity.Contract;
import com.vast.admin.entity.ContractDatum;
import com.vast.admin.entity.dto.ContractDatumDto;
import com.vast.admin.entity.enums.QueryOrderEnum;
import com.vast.admin.service.ContractService;
import com.vast.admin.service.impl.ContractDatumService;
import com.vast.admin.service.impl.LawsuitFlowChangeRecordService;
import com.vast.common.annotation.Log;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.FileUtils;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.utils.StringUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import com.vast.system.utils.OssUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaohao
 * @since 2020-04-09
 */
@Api(value="合同资料操作相关的api",tags= {"合同资料模块"})
@RestController
@RequestMapping("/manager/contractDatum")
public class ContractDatumController extends ApiController {

	@Autowired
	private ContractDatumService service;

	@Autowired
	private ContractService contractService;

	@Autowired
	private OssUtils ossUtils;

	@Autowired
	private LawsuitFlowChangeRecordService flowChangeRecordService;

	@ApiOperation(value = "查询列表信息", notes = "通过条件查询列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
			@ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
			@ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
			@ApiImplicitParam(paramType = "query", name = "queryItems", value = "查询条件,json集合", defaultValue = "account,EQ,13419519796;", dataType = "String", required = false),
			@ApiImplicitParam(paramType = "query", name = "orderField", value = "排序字段,多个','分隔", defaultValue = "create_time", dataType = "String",  required = false),
			@ApiImplicitParam(paramType = "query", name = "order", value = "排序类型: ASC/DESC", dataType = "QueryOrderEnum",  required = false)
	})
	@GetMapping("/page")
	public Result<IPage<ContractDatum>> page(@ApiParam(hidden = true) Page<ContractDatum> page, boolean listMode, String queryItems, String orderField, QueryOrderEnum order) {
		return new Result<IPage<ContractDatum>>(MessageCode.SUCCESS, "获取成功", service.queryList(page, listMode, queryItems, orderField,order));

	}

	@ApiOperation(value = "添加或更新", notes = "添加或更新")
	@UseToken
	@Log(title = "添加或更新合同资料")
	@PostMapping("/addOrUpdate")
	public Result<Boolean> addOrUpdate(@RequestBody String contractDatumJson) {
		if(contractDatumJson != null && !StringUtils.isEmpty(contractDatumJson)) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
			Gson gson = new Gson();
			ContractDatumDto contractDatumDto = gson.fromJson(contractDatumJson, new TypeToken<ContractDatumDto>() {}.getType());
			if(contractDatumDto != null){

				if(contractDatumDto.getContractId() != null){
					Contract contract = contractService.getById(contractDatumDto.getContractId());
					if(contract != null){
						//更新文档资料上传状态
						contract.setDatumStatus(contractDatumDto.getUploadStatus());
						contractService.updateById(contract);
					}else{
						return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
					}
				}else{
					return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
				}
				ContractDatumDto.FileInfo[] fileInfos = contractDatumDto.getFileList();
				if(fileInfos != null && fileInfos.length > 0){
					Long contractId = contractDatumDto.getContractId();
					List<ContractDatum> contractDatumList = new ArrayList<>();
					for (ContractDatumDto.FileInfo fileInfo : fileInfos) {
						ContractDatum contractDatum = new ContractDatum();
						contractDatum.setFileName(fileInfo.getName());
						if(FileUtils.isImage(fileInfo.getUrl())){
							contractDatum.setType(1);//图片
						}else{
							contractDatum.setType(2);//文件
						}
						contractDatum.setOssFilePath(fileInfo.getUrl());
						contractDatum.setStatus(1);
						contractDatum.setContractId(contractDatumDto.getContractId());
						contractDatum.setDatumType(fileInfo.getType());
						contractDatum.setRemark(contractDatumDto.getRemark());
						contractDatum.setContractId(contractId);
						contractDatum.setValid(1);
						contractDatum.setCreateBy(manager.getUserId());
						contractDatumList.add(contractDatum);
					}
					service.saveBatch(contractDatumList);
				}
			}
			return new Result<Boolean>(MessageCode.SUCCESS, "上传成功",true);
		}else {
			return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
		}
	}

	@ApiOperation(value = "添加或更新", notes = "添加或更新")
	@UseToken
	@Log(title = "添加或更新合同资料上传状态")
	@PostMapping("/updateContractDatumStatus")
	public Result<Boolean> updateContractDatumStatus(@RequestParam("uploadStatus") int uploadStatus, @RequestParam("contractId")long contractId){
		if(contractId > 0){
			Contract contract = contractService.getById(contractId);
			if(contract != null){
				//更新文档资料上传状态
				contract.setDatumStatus(uploadStatus);
				contractService.updateById(contract);
				return new Result<Boolean>(MessageCode.SUCCESS, "上传成功",true);
			}else{
				return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
			}
		}
		return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
	}



	@ApiOperation(value = "通过id获取信息", notes = "通过id获取信息")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true)})
	@GetMapping("/info/{id}")
	@UseToken
	@Log(title = "通过id获取信息")
	public Result<ContractDatum> info(@PathVariable("id") Long id) {
		if(id != null && id.longValue() > 0){
			SysUser manager = SecurityUtils.getLoginUser().getUser();
			ContractDatum bean = service.info(manager,id);
			if(bean != null){
				return new Result<ContractDatum>(MessageCode.SUCCESS, "获取成功", bean);
			}else{
				return new Result<ContractDatum>(MessageCode.ERROR, "获取失败");
			}
		}
		return new Result<ContractDatum>(MessageCode.PARAM_ERROR, "id不能为空");
	}

	@ApiOperation(value = "删除", notes = "删除")
	@UseToken
	@Log(title = "删除")
	@DeleteMapping("/delete")
	public Result<Boolean> delete( @RequestBody @ApiParam(name="ids",value="数据id数组,以','分隔",required=true) Long[] ids) {
		if(ids != null) {
			SysUser manager = SecurityUtils.getLoginUser().getUser();
			boolean flag  = service.delete(manager, ids);
			if(flag){
				return new Result<Boolean>(MessageCode.SUCCESS, "删除成功");
			}else{
				return new Result<Boolean>(MessageCode.ERROR, "删除失败");
			}
		}else {
			return new Result<Boolean>(MessageCode.PARAM_ERROR, "参数错误");
		}
	}

	@ApiOperation(value = "启/禁用合同资料", notes = "启/禁用合同资料")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "id", value = "数据id", dataType = "Long", required = true),
			@ApiImplicitParam(paramType = "query", name = "status", value = "状态0-禁用 1-启用", dataType = "Integer", defaultValue="1", required = true)
	})
	@UseToken
	@PostMapping("/enable")
	public Result enable(Long id, Integer status) {
		ContractDatum bean = service.getById(id);
		if(bean != null){
			bean.setStatus(status);
			service.saveOrUpdate(bean);
			return new Result(MessageCode.SUCCESS, "修改成功",true);
		}else{
			return new Result(MessageCode.ERROR, "此数据不存在");
		}

	}

}
